/*
 * sc_s.c
 *
 * Created: 2012-07-05 20:06:45
 *  Author: Antrykot
 */ 

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include "../sc-s/utils.h"
#include "../sc-s/trans-s.h"
#include "../sc-k/SWLcd.h"
#include <avr/sleep.h>
#include <avr/power.h>

#include <stdlib.h>
 
u8 kom_flag = 0;

ISR(SIG_INTERRUPT0)
{
	TR_K_DISABLE;
	LG_ON;
	trans_k();
	WAITMS(4);
	LG_OFF;
	EIFR |= (1 << INTF0);
	TR_K_ENABLE;
	LG_OFF;
	kom_flag = 1;
}

ISR(SIG_OVERFLOW1)
{
	TimerH++;
}

ISR(SIG_OVERFLOW0)
{
	t0_cnt ++;
	
	if (t0_cnt > 15)	// 30ms (256us * 8div * 15)
	{
		t0_cnt = 0;
		timeout = 1;
		T0STOP;
	}
}


ISR(SIG_OUTPUT_COMPARE2A)		// wake me up
{
	sleep_disable();
	time_cnt++;
}


int main(void)
{
	setup_io();
	
	ALARM1_OFF;
	ALARM2_OFF;

	LED_A_ON;
	LED_W_ON;
	
	AWP_SET_POWERFLAG;
	
	setup_int();
	_delay_ms(100);
	lcd_init();
	init_eeprom();


	TR_K_ENABLE;


	sei();
	intro();
	
	awp_flags = 0;
	
	if (Pro == PRO_SERWIS)
	{
		pin_ok = 0;
		ref_pin = 2512;
		while(!pin_ok)
		{
			pin_loop();
			if (pin_ok == 1)
			{
				lcd_home();
				lcd_print(" SERWIS ");
				WAITMS(4000);
				Pro = PRO_SERWIS;
			}
			if (pin_ok == 255)
			{
				lcd_home();
				lcd_print("  BRAK  ");
				WAITMS(1000);
				lcd_print("ZEZWOLEN");
				WAITMS(1000);
				Pro = PRO_WORK;
			}
			WAITMS(100);
		}
	}
	
    while(1)
    {
	
		
		check_keyboard();
		if (!PLU || !MIN || !ENT)
		{
			static u8 init_cnt = 0;
			init_cnt++;
			if (init_cnt > 10)
			{
				lcd_init();
				WAITMS(1);
				lcd_home();
				init_cnt = 0;
			}
		}

		
		WAITMS(100);
		//trans_p();
		/*
		lcd_clear();
		char txt[16];
		itoa(rk_data,txt,10);
		lcd_print(txt);


		itoa(rk_addr,txt,10);
		lcd_print("/");
		lcd_print(txt);
		lcd_print("-----");
		*/
		work();
		
		if (tk)
		{
			//LR_ON;
			tk = 0;
			interpret_sk();
			//LR_OFF;
		}
		put_to_sleep();
    }
}


void put_to_sleep()
{
	set_sleep_mode(SLEEP_MODE_IDLE);
	sleep_enable();
	sleep_mode();
}

#ifdef MEDIA_PS

void calc_dac_out()
{
	if (!uout)
	{
		napb = 0;
		napa = 0;
		return;
	}
	if (uout == 2)	// in calib;
	return;
	
	u16 tmp;
	float fnap;
	
	switch(param_trans)
	{
		case 0x00:		// presure
		{
			tmp = pom_pr & 0x3FFF;
		
			if (tmp < pr_range_l )
			{
				napb = U2_35;	//350mV
				napa = U1_35;
				return;
			}
		
			if (tmp > pr_range_h )
			{
				napb = U2_21;	//2100mV
				napa = U1_21;
				return;
			}
		

			fnap = U1_20 - U1_40;
			fnap = fnap / (pr_range_h - pr_range_l) * (tmp - pr_range_l) + U1_40;
			napa = fnap;

			fnap = U2_20 - U2_40;
			fnap = fnap / (pr_range_h - pr_range_l) * (tmp - pr_range_l) + U2_40;
			napb = fnap;
			break;
		}		
		case 0x01:		// humidity
		{
			tmp = pom_rh & 0x3FFF;
			
			if (tmp < 10 )
			{
				napb = U2_35;	//350mV
				napa = U1_35;
				return;
			}
			
			if (tmp > 1000 )
			{
				napb = U2_21;	//2100mV
				napa = U1_21;
				return;
			}
			

			fnap = U1_20 - U1_40;
			fnap = fnap / (1000) * (tmp) + U1_40;
			napa = fnap;

			fnap = U2_20 - U2_40;
			fnap = fnap / (1000) * (tmp) + U2_40;
			napb = fnap;
			break;
		}
		
		case 0x02:		// temperature
		{
			tmp = pom_tm & 0x3FFF;
			
			if (tmp < 1 )		// 
			{
				napb = U2_35;	//350mV
				napa = U1_35;
				return;
			}
			
			if (tmp > 700 )		// +50*C
			{
				napb = U2_21;	//2100mV
				napa = U1_21;
				return;
			}
			

			fnap = U1_20 - U1_40;
			fnap = fnap / (700) * (tmp) + U1_40;
			napa = fnap;

			fnap = U2_20 - U2_40;
			fnap = fnap / (700) * (tmp) + U2_40;
			napb = fnap;
			break;
		}
		
		
	}
	

	
	
}

#endif


#ifdef MEDIA_PS

void config_menu()
{

	switch(calib_ptr_deep)
	{
		case 0x00:
		{
			lcd_home();
			lcd_print(" KONFIG ");
			WAITMS(1000);
			lcd_home();
			lcd_print("SET[-/+]");
			WAITMS(1000);
			calib_ptr_deep++;
			break;
		}
		case 0x01:
		{
			lcd_home();
			switch(Ksp)
			{
				case 0:
				lcd_print(" BRAK   ");
				break;
				case 1:
				lcd_print(" KSP-2  ");
				break;
				case 2:
				lcd_print(" KSP-3  ");
				break;
				
			}
			if (!PLU)
			{
				Ksp++;
			}
			
			if (!MIN)
			{
				Ksp--;
			}
			
			if (Ksp > 2)
			Ksp = 0;

			if (!ENT)
			{
				_write(EEP_KSP,Ksp);
				
				if (Ksp == 0 || Ksp == 1)
				{
					tk_data = 0;		// analog
					tk_addr = K_ADDR_KSP;
				}
				else
				{
					tk_data = 1;		// digtrans
					tk_addr = K_ADDR_KSP;
				}
				
				calib_ptr_deep = 5;
			}
			break;
		}
		case 0x05:
		{
			lcd_home();
			lcd_print("KCL ");
			if (Kcl)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Kcl = 1;
			}
			if (!MIN)
			{
				Kcl = 0;
			}
			if (!ENT)
			{
				_write(EEP_KCL,Kcl);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x06:
		{
			lcd_home();
			lcd_print("WY-U ");
			if (uout)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				uout = 1;
			}
			if (!MIN)
			{
				uout = 0;
			}
			if (!ENT)
			{
				_write(EEP_UOUT,uout);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x07:
		{
			lcd_home();
			lcd_print("LCD ");
			if (lcdmode)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("TIME");
			}
			if (!PLU)
			{
				lcdmode = 1;
			}
			if (!MIN)
			{
				lcdmode = 0;
			}
			if (!ENT)
			{
				_write(EEP_LCDM,lcdmode);
				calib_ptr_deep += 1;
				sh_flag = _read(EEP_SH_FLAG);

			}
			break;
		}
#ifdef MEDIA_PS
		case 0x08:
		{
			
			lcd_home();
			lcd_print("hPa ");
			if (sh_flag & 0x01)
			{
				lcd_print(" TAK");
			}
			else
			{
				lcd_print(" NIE");
			}
			
			if (!PLU)
			{
				sh_flag |= 0x01;
			}
			if (!MIN)
			{
				sh_flag &= ~0x01;
			}
			if (!ENT)
			{
				_write(EEP_SH_FLAG,sh_flag);
				calib_ptr_deep += 1;
				sh_flag = _read(EEP_SH_FLAG);

			}
			break;
		}
		case 0x09:
		{
			
			lcd_home();
			lcd_print("%RH ");
			if (sh_flag & 0x02)
			{
				lcd_print(" TAK");
			}
			else
			{
				lcd_print(" NIE");
			}
			
			if (!PLU)
			{
				sh_flag |= 0x02;
			}
			if (!MIN)
			{
				sh_flag &= ~0x02;
			}
			if (!ENT)
			{
				_write(EEP_SH_FLAG,sh_flag);
				calib_ptr_deep += 1;
				sh_flag = _read(EEP_SH_FLAG);

			}
			break;
		}		
		case 0x0A:
		{
			
			lcd_home();
			lcd_print("TEMP");
			if (sh_flag & 0x04)
			{
				lcd_print(" TAK");
			}
			else
			{
				lcd_print(" NIE");
			}
			
			if (!PLU)
			{
				sh_flag |= 0x04;
			}
			if (!MIN)
			{
				sh_flag &= ~0x04;
			}
			if (!ENT)
			{
				if (! sh_flag )
				{
					lcd_clear();
					lcd_print("hPa,RH,T");
					WAITMS(1000);
					sh_flag = 7;	// if none, all
				}
				
				_write(EEP_SH_FLAG,sh_flag);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x0B:
		{
			lcd_home();
			lcd_print("ZAKRES N");
			WAITMS(1000);
			calib_ptr_deep += 1;
			pr_range_l = read_word(EEP_PR_RANGE_L);
			break;
		}
		case 0x0C:
		{
			
			lcd_home();
			char str[8];
			itoa(pr_range_l / 10,str,10);
			lcd_print(str);
			lcd_print("hPa ");
			
			if (!PLU)
			{
				pr_range_l += 100;
			}
			if (!MIN)
			{
				pr_range_l -= 100;
			}
			if (pr_range_l < 9000)
			{
				pr_range_l = 9000;
			}
			if (pr_range_l > 12000)
			{
				pr_range_l = 12000;
			}
			
			if (!ENT)
			{
				write_word(EEP_PR_RANGE_L,pr_range_l);
				calib_ptr_deep += 1;
			}
			break;
		}
		case 0x0D:
		{
			lcd_home();
			lcd_print("ZAKRES W");
			pr_range_h = read_word(EEP_PR_RANGE_H);
			WAITMS(1000);
			calib_ptr_deep += 1;
			break;
		}

		case 0x0E:
		{	
			
			lcd_home();
			char str[8];
			itoa(pr_range_h / 10,str,10);
			lcd_print(str);
			lcd_print("hPa  ");
			
			if (!PLU)
			{
				pr_range_h += 100;
			}
			if (!MIN)
			{
				pr_range_h -= 100;
			}
			if (pr_range_h < pr_range_l)
			{
				pr_range_h = pr_range_l + 100;
			}
			if (pr_range_h > 13000)
			{
				pr_range_h = 13000;
			}
			
			if (!ENT)
			{
				write_word(EEP_PR_RANGE_H,pr_range_h);
				calib_ptr_deep += 1;
			}
			break;
		}
		case 0x0F:
		{
			lcd_home();
			
			if (param_trans == 0x00)
			{
				lcd_print("hPa");
			}
			if (param_trans == 0x01)
			{
				lcd_print("%RH");
			}
			if (param_trans == 0x02)
			{
				lcd_print("TMP");
			}
			if (!PLU)
				param_trans ++;
			if (!MIN)
				param_trans --;
				
			if (param_trans > 0x02)
				param_trans = 0x00;

			
			lcd_print(">KSP2");
			if (!ENT)
			{
				_write(EEP_PARAM,param_trans);
				if (param_trans == 0)
					sh_flag |= 0x01;
				if (param_trans == 1)
					sh_flag |= 0x02;
				if (param_trans == 2)
					sh_flag |= 0x04;
				
				_write(EEP_SH_FLAG,sh_flag);	// update sh_flag
				calib_ptr_deep++;
			}
			break;
		}
		case 0x10:
		{
			
			lcd_home();
			lcd_print("ZAPISANE");
			tk_addr = K_ADDR_LRANGE;
			tk_data = pr_range_l;
			WAITMS(1500);
			tk_addr = K_ADDR_HRANGE;
			tk_data = pr_range_h;
			WAITMS(500);
			tk_addr = K_ADDR_PARAM;
			tk_data = param_trans;
			Pro = PRO_WORK;
			intro();
			break;
		}
#endif
	}
	
	if (!PLU || !MIN)
	{
		WAITMS(100);
	}
	
	while( !ENT);
	
}

#endif


#ifdef MEDIA_PS

void display_name()
{
	lcd_print(" SC-PS  ");
	tk_addr = K_ADDR_PARAM;
	tk_data = param_trans;
}
#define INTRO_DELAY	1000

void display_alarms()
{
	char str[8];
	lcd_home();
	lcd_print("CISNIE. ");

	WAITMS(INTRO_DELAY);
	
	itoa(pr_range_h/10,str,10);
	lcd_home();
	lcd_print(str);
	lcd_print("hPa- ");
	WAITMS(INTRO_DELAY);
	lcd_home();
	lcd_print("-");
	itoa(pr_range_l/10,str,10);
	lcd_print(str);
	lcd_print("hPa  ");

	WAITMS(INTRO_DELAY);
	
	fuse2(str,AL1);
	lcd_print(str);
	WAITMS(INTRO_DELAY);
	lcd_home();
	if (sh_flag & 0x01)
	{
		lcd_print("hPa ");
	}
	else
	{
		lcd_print("--- ");
	}
	if (sh_flag & 0x02)
	{
		lcd_print("RH ");
	}
	else
	{
		lcd_print("-- ");
	}
	if (sh_flag & 0x04)
	{
		lcd_print("T ");
	}
	else
	{
		lcd_print("- ");
	}
	WAITMS(INTRO_DELAY);
	lcd_clear();
	switch(param_trans)
	{
		case 0:
		{
			lcd_print("hPa>KSP2");
			break;
		}
		case 1:
		{
			lcd_print("%RH>KSP2");
			break;
		}
		case 2:
		{
			lcd_print(" T >KSP2");
			break;
		}
	}
	
	
	WAITMS(INTRO_DELAY);
}

#endif


u16 Nr;
u8 Ksp;
u8 Awp;
u16 AL1;
u16 AL2;
u8 Kcl;

u16 U1_21;
u16 U1_20;
u16 U1_40;
u16 U1_35;

u16 U2_21;
u16 U2_20;
u16 U2_40;
u16 U2_35;

u8 uout;
u8 lcdmode;
u16 wk_zas;
u16 wk_aku;
u16 stw1;
u16 stw2;
u16 stn;

u8 serwis_flag;

u8 Pro;

u8 pin_ok = 0;
u16 ref_pin = 0;

u8 timeout = 0;
u8 t0_cnt = 0;

u16 pr_range_l = 9000;
u16 pr_range_h = 11000;

u8 param_trans = 0;

i16 corr_t = 0;
i16 corr_rh = 0;


char cuCharA[8] = {
	0b01110,
	0b10001,
	0b10001,
	0b10001,
	0b11111,
	0b10001,
	0b10001,
	0b00010
};

char cuCharL[8] = {
	0b10000,
	0b10010,
	0b10100,
	0b11000,
	0b10000,
	0b10000,
	0b11111,
	0b00000
};

